iT邦幫忙

2024 iThome 鐵人賽

DAY 5
0
Software Development

十年後重讀作業系統恐龍本系列 第 5

ch3圖3.17,18-說明POSIX共用記憶體API的行程

  • 分享至 

  • xImage
  •  

合作行程例子:「生產者-消費者」問題

名詞定義:

  • 獨立行程(independent process):一個行程無法影響其它行程的執行且不受其他的行程影響。
  • 合作行程(cooperating process):一個行程能夠影響其它行程,或是受其他的行程影響。
  • 行程間通訊(interprocess communication, IPC):讓行程彼此間交換資料和訊息,有共用記憶體(shared memory)和訊息傳遞(message passing)兩個基本模式。
  • 生產者(producer)行程:產生資訊。
  • 消費者(consumer)行程:消費掉這些資訊。

shm-posix-producer.c

/**
 * Simple program demonstrating shared memory in POSIX systems.
 *
 * This is the producer process that writes to the shared memory region.
 *
 * Figure 3.17
 *
 * @author Silberschatz, Galvin, and Gagne
 * Operating System Concepts  - Ninth Edition
 * Copyright John Wiley & Sons - 2013
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>

int main()
{
	const int SIZE = 4096; /* 定義共享記憶體的大小為 4096 bytes */
	const char *name = "OS"; /* 定義共享記憶體段的名稱 */
	const char *message0= "Studying ";
	const char *message1= "Operating Systems ";
	const char *message2= "Is Fun!";
    
	int shm_fd; /* 儲存共享記憶體段的檔案描述符 */
	void *ptr; /* 用於儲存映射區域的指標 */

	/* create the shared memory segment */
    /* 創建或打開一個共享記憶體段,名稱為 "OS",具有讀寫權限 */
	shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);

	/* configure the size of the shared memory segment */
    /* 設置共享記憶體段的大小為 4096 bytes */
	ftruncate(shm_fd,SIZE);

	/* now map the shared memory segment in the address space of the process */
    /* 將共享記憶體段映射到進程的地址空間,允許讀寫 */
	ptr = mmap(0,SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
    /* 如果映射失敗,打印錯誤信息並返回 -1 */
	if (ptr == MAP_FAILED) {
		printf("Map failed\n");
		return -1;
	}

	/**
	 * Now write to the shared memory region.
 	 *
	 * Note we must increment the value of ptr after each write.
	 */
	sprintf(ptr,"%s",message0); /* 將 message0 寫入共享記憶體 */
	ptr += strlen(message0); /* 更新指標位置 */
	sprintf(ptr,"%s",message1);
	ptr += strlen(message1);
	sprintf(ptr,"%s",message2);
	ptr += strlen(message2);

	return 0;
}

shm-posix-consumer.c

/**
 * Simple program demonstrating shared memory in POSIX systems.
 *
 * This is the consumer process
 *
 * Figure 3.18
 *
 * @author Gagne, Galvin, Silberschatz
 * Operating System Concepts - Ninth Edition
 * Copyright John Wiley & Sons - 2013
 */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>

int main()
{
	const char *name = "OS";
	const int SIZE = 4096;

	int shm_fd;
	void *ptr;
	int i;

	/* open the shared memory segment */
    /* 以只讀方式打開共享記憶體段 */
	shm_fd = shm_open(name, O_RDONLY, 0666);
    /* 如果打開失敗,打印錯誤信息並退出程式 */
	if (shm_fd == -1) {
		printf("shared memory failed\n");
		exit(-1);
	}

	/* now map the shared memory segment in the address space of the process */
    /* 將共享記憶體段映射到進程的地址空間,允許讀取 */
	ptr = mmap(0,SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);
    /* 如果映射失敗,打印錯誤信息並退出程式 */
	if (ptr == MAP_FAILED) {
		printf("Map failed\n");
		exit(-1);
	}

	/* now read from the shared memory region */
    /* 從共享記憶體中讀取資料並打印到標準輸出 */
	printf("%s",ptr);

	/* remove the shared memory segment */
    /* 刪除共享記憶體段,如果失敗則打印錯誤信息並退出程式 */
	if (shm_unlink(name) == -1) {
		printf("Error removing %s\n",name);
		exit(-1);
	}

	return 0;
}

Terminal
編譯並執行

gcc -o shm-posix-producer shm-posix-producer.c
gcc -o shm-posix-consumer shm-posix-consumer.c
./shm-posix-producer
./shm-posix-consumer

結果:
https://ithelp.ithome.com.tw/upload/images/20240919/20168766jCDRITfSv6.png

參考:


上一篇
ch3圖3.9-使用UNIX fork()系統呼叫產生獨立行程
下一篇
ch3圖3.20,21,22-客戶—伺服器系統的通信:使用socket的通信
系列文
十年後重讀作業系統恐龍本12
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言